/**
 * Copyright Yao Xuanyu @ CST04, Tsinghua University
 */
package cn.edu.tsinghua.sthuapp;

import java.io.*;
import java.util.*;

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.entity.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.message.*;
import org.apache.http.util.*;

import android.util.Log;

/**
 * Utilities for Android applications.
 * 
 * @author Yao Xuanyu
 * @version 4.0
 */
public class Util {

	/**
	 * Get particular event by ID.
	 * 
	 * @param id Event ID.
	 * @return Specific event/activity.
	 * @author YaoXuanyu
	 * @throws IOException Internet problem.
	 */
	public static Event getEvent(int id) throws IOException {
		// Setup http request.
		HttpPost request = new HttpPost(Constant.SERVER);
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("operate", String.valueOf(Constant.QUERY)));
		parameters.add(new BasicNameValuePair("content", String.valueOf(Constant.OBJECT)));
		parameters.add(new BasicNameValuePair("id", String.valueOf(id)));
		HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");
		request.setEntity(entity);
		HttpClient client = new DefaultHttpClient();
		// Get and parse http response.
		HttpResponse response = client.execute(request);
		Event event = new Event();
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			String JSON = EntityUtils.toString(response.getEntity());
			event.parseEvent(JSON);
			if (JSON.equals("")) {
				return null;
			}
		}
		// Translate event type.
		for(int i = 0; i < Constant.CLASSIFICATION_ID.length; i++) {
			if(event.getClassification() == Constant.CLASSIFICATION_ID[i]) {
				event.setClassification(i);
			}
		}
		return event;
	}

	/**
	 * Get particular event list by page, class ID and keyword.
	 * 
	 * @param id Classification in client sequence.
	 * @param page Page number.
	 * @param keyword Keyword.
	 * @return Specific event/activity list.
	 * @author YaoXuanyu
	 * @throws IOException Internet problem.
	 */
	public static Vector<Event> getEvents(int id, int page, String keyword) throws IOException {
		// Setup http request.
		HttpPost request = new HttpPost(Constant.SERVER);
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("operate", String.valueOf(Constant.QUERY)));
		parameters.add(new BasicNameValuePair("content", String.valueOf(Constant.LIST)));
		parameters.add(new BasicNameValuePair("id", String.valueOf(Constant.CLASSIFICATION_ID[id])));
		parameters.add(new BasicNameValuePair("page", String.valueOf(page)));
		parameters.add(new BasicNameValuePair("keyword", keyword));
		HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");
		request.setEntity(entity);
		HttpClient client = new DefaultHttpClient();
		// Get and parse http response.
		HttpResponse response = client.execute(request);
		Vector<Event> events = new Vector<Event>();
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			String result[] = EntityUtils.toString(response.getEntity()).split("\n");
			for(int i = 0; i < result.length; i++) {
				if (result[i].equals("")) {
					continue;
				}
				Event event = new Event();
				event.parseEvent(result[i]);
				// Translate event type.
				for(int k = 0; k < Constant.CLASSIFICATION_ID.length; k++) {
					if(event.getClassification() == Constant.CLASSIFICATION_ID[k]) {
						event.setClassification(k);
					}
				}
				events.add(event);
			}
		}
		return events;
	}
	
	/**
	 * Get particular event list by page and class ID.
	 * 
	 * @see Util#getEvents(int, int, String)
	 * @param id Classification in client sequence.
	 * @param page Page number.
	 * @return Specific event/activity list.
	 * @author YaoXuanyu
	 * @throws IOException Internet problem.
	 */
	public static Vector<Event> getEvents(int id, int page) throws IOException {
		return getEvents(id, page, "");
	}
}
